﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Dao.Line;
using SMTC.Model.Domain;
using System.Data.Common;
using SMTC.Model.Model;

namespace SMTC.BO.Line
{
    public class LineProccessServices
    {
        private LineProccessDao _dao = new LineProccessDao();
        /// <summary>
        /// 插入生产线
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool InsertLine(LineDomain domain)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                domain = this.ProccessCapacity(domain);
                operResult = operResult && _dao.InsertLine(domain.line, ref dbt);
                foreach (var item in domain.puList)
                {
                    operResult = operResult && _dao.InsertLinesProductUnit(item, ref dbt);
                }

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 删除生产线
        /// </summary>
        /// <param name="LineID"></param>
        /// <returns></returns>
        public bool DeleteLine(string LineID)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                operResult = operResult && _dao.DeleteLine(LineID, ref dbt);
                operResult = operResult && _dao.DeleteLinesProductUnit(LineID, ref dbt);

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 更新生产线信息
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool UpdateLine(LineDomain domain)
        {
            DbTransaction dbt = null;
            bool operResult = true;

            try
            {
                domain = this.ProccessCapacity(domain);
                operResult = operResult && _dao.UpdateLine(domain.line, ref dbt);
                operResult = operResult && _dao.DeleteLinesProductUnit(domain.line.LineID, ref dbt);
                foreach (var item in domain.puList)
                {
                    operResult = operResult && _dao.InsertLinesProductUnit(item, ref dbt);
                }

                if (operResult == true)
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }

        /// <summary>
        /// 计算生产线能力
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public LineDomain ProccessCapacity(LineDomain domain)
        {
            var result = from item in domain.puList
                         group item by item.LineID into g
                         select new SMT_ProductionUnitModel
                             {
                                 PUCapacity = g.Min(a => a.PUCapacity)
                             };
            List<SMT_ProductionUnitModel> list = result.Cast<SMT_ProductionUnitModel>().ToList<SMT_ProductionUnitModel>();
            if (list != null && list.Count > 0)
            {
                domain.line.LineCapacity = list[0].PUCapacity;
            }
            return domain;
        }
    }
}
